package com.kifas.service.dataaccess;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.SatuanEntity;

@Repository
public class SatuanDao {

	@Autowired
	private SessionFactory sessionFactory;

	private final static String ALL_SATUAN = "from SatuanEntity ";
	private final static String SATUAN_BY_ID = ALL_SATUAN
			+ "where idSatuan = :idSatuan ";

	@SuppressWarnings("unchecked")
	public List<SatuanEntity> getSatuans() {
		return sessionFactory.getCurrentSession().createQuery(ALL_SATUAN)
				.list();
	}

	public SatuanEntity getSatuanById(Long id) {
		return (SatuanEntity) sessionFactory.getCurrentSession()
				.createQuery(SATUAN_BY_ID).setLong("idSatuan", id)
				.uniqueResult();
	}

	public void insert(SatuanEntity satuan) throws Exception {
		sessionFactory.getCurrentSession().save(satuan);
	}
	
	public void update(SatuanEntity satuan) throws Exception {
		sessionFactory.getCurrentSession().update(satuan);
	}
}